﻿namespace SharePoint_2010_Powershell_Utility
{
    using System;
    using System.Windows.Forms;
    using System.IO;
    using System.Threading;
    using System.Diagnostics;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;

    public partial class PowershellUtility : Form
    {
        #region Constructor

        public PowershellUtility()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        private void PowershellUtility_Load(object sender, EventArgs e)
        {
            try
            {
                PopulatePowershellCommands();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCompose_Click(object sender, EventArgs e)
        {
            try
            {
                txtPowershellCommandText.Clear();
                txtPowershellCommandText.Text = this.ComposeCommand();
            }
            catch(Exception ex)
            {
                txtStatus.Text += Environment.NewLine + ex.Message;
            }
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                PowerShell ps = PowerShell.Create();
                ps.AddScript("Add-PsSnapin Microsoft.SharePoint.PowerShell");
                string pCmd = this.ComposeCommand(); ;
                ps.AddScript(pCmd);
                ps.AddCommand("Out-String");

                foreach (string s in ps.Invoke<string>())
                {
                    txtStatus.Text += Environment.NewLine + s;
                }

                this.Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                txtStatus.Text += Environment.NewLine + ex.Message;
            }
        }

        private void btnIISReset_Click(object sender, EventArgs e)
        {
            try
            {                
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "cmd";
                startInfo.Arguments = "/k iisreset /RESTART";
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = false;              

                Process process = Process.Start(startInfo);
                process.WaitForExit();
                process.Close();

                txtStatus.Text += Environment.NewLine + "IIS Reset Completed.";
            }
            catch (Exception ex)
            {
                txtStatus.Text += Environment.NewLine + ex.Message;
            }
        }        

        private void btnClearStatus_Click(object sender, EventArgs e)
        {
            try
            {
                txtStatus.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                if (cboPowershellCommands.SelectedIndex != -1)
                {
                    PowerShell ps = PowerShell.Create();
                    ps.AddScript("Add-PsSnapin Microsoft.SharePoint.PowerShell");
                    string pCmd = "get-help " + cboPowershellCommands.SelectedItem;
                    ps.AddScript(pCmd);
                    ps.AddCommand("Out-String");

                    foreach (string s in ps.Invoke<string>())
                    {
                        txtStatus.Text += Environment.NewLine + s;
                    }

                    this.Cursor = Cursors.Default;
                }
            }
            catch (Exception ex)
            {
                txtStatus.Text += Environment.NewLine + ex.Message;
            }
        }

        #endregion                

        #region Private Methods

        private void PopulatePowershellCommands()
        {
            cboPowershellCommands.Items.Clear();
                     
            cboPowershellCommands.Items.Add("disable-spfeature");
            cboPowershellCommands.Items.Add("enable-spfeature");            
            cboPowershellCommands.Items.Add("install-spfeature");
            cboPowershellCommands.Items.Add("uninstall-spfeature");
            cboPowershellCommands.Items.Add("get-spfeature");
            cboPowershellCommands.Items.Add("get-spfeature -web");
            cboPowershellCommands.Items.Add("get-spfeature -site");
            cboPowershellCommands.Items.Add("get-spfeature -webapplication"); 
        }

        private string ComposeCommand()
        {
            string pscommand = cboPowershellCommands.SelectedItem.ToString();

            if (string.Equals(pscommand, "enable-spfeature") || string.Equals(pscommand, "disable-spfeature"))
            {
                pscommand = pscommand + " -identity \"" + txtFeatureNameGuid.Text + "\"" + " -url \"" + txtSiteUrl.Text + "\"";
            }

            if (string.Equals(pscommand, "install-spfeature") || string.Equals(pscommand, "uninstall-spfeature"))
            {
                pscommand = pscommand + " -path \"" + txtFeatureNameGuid.Text + "\"";
            }

            if (pscommand.Contains("get-spfeature"))
            {
                pscommand += " \"" + txtSiteUrl.Text + "\"";
            }
            
            return pscommand;
        }

        #endregion      
    }
}
